//
//  HMStatusRetweetView.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/20.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit
//被转发微博
class HMStatusRetweetView: UIView {

    var retweetStatus: HMStatus? {
        // 当别人设置被转发微博的内容,我们来设置相应的内容到view上面
        // 被转发的微博文本内容 = @被转发微博的用户名称: 被转发微博的文本内容
        didSet {
            //设置微博位置内容
            let retweetName = retweetStatus?.user?.screen_name ?? "无名"
            let reweetText = retweetStatus?.text ?? "无内容"
            let reweetContent = "@\(retweetName): \(reweetText)"
            reweetLabel.text = reweetContent
            
            //设置配图
            reweetPictureView.pictureURLs = retweetStatus?.storePictureURLs
            // 当没有图片的时候,HMStatusRetweetView距离retweetLabel的底部不需要8的间距
            // snp_updateConstraints: 前提是参照的view和参照的约束没有变化才能updateConstraints
            reweetPictureView.snp_makeConstraints { (make) in
                let magin = retweetStatus?.storePictureURLs == nil ? 0 : HMStatusCellMargin
                make.top.equalTo(reweetLabel.snp_bottom).offset(magin)
            }
        }
    }
//初始化init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //绘制控件
    private func prepareUI() {
    addSubview(bkgButton)
    addSubview(reweetLabel)
    addSubview(reweetPictureView)
        
    //添加约束
    bkgButton.snp_makeConstraints { (make) in
        make.edges.equalTo(self)
        }
        
    reweetLabel.snp_makeConstraints { (make) in
        make.leading.top.equalTo(self).offset(HMStatusCellMargin)
        make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
    reweetPictureView.snp_makeConstraints { (make) in
        make.leading.equalTo(self).offset(HMStatusCellMargin)
        make.top.equalTo(reweetLabel.snp_bottom).offset(HMStatusCellMargin)
        make.size.equalTo(CGSizeZero)
        }
        
    //为 view添加底部约束
    self.snp_makeConstraints { (make) in
        make.bottom.equalTo(reweetPictureView.snp_bottom).offset(HMStatusCellMargin)
        }
    
    }
//MARK: 懒加载
    //背景按钮（点击被转发微博背景会显示选中状态）
    private lazy var bkgButton: UIButton = {
    let button = UIButton()
    //设置按钮
    button.setBackgroundImage(UIImage.init(named: "retweet_background"), forState: UIControlState.Normal)
    button.setBackgroundImage(UIImage.init(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
    
    return button
    }()
    //被转发微博文字内容
    private lazy var reweetLabel: UILabel = {
    let label = UILabel(fontSize: HMStatusContentFontSize, textColor: UIColor.darkGrayColor())
    label.numberOfLines = 0
    return label
    }()
    //被转发微博图片
    private lazy var reweetPictureView: HMStatusPictureView = HMStatusPictureView()
    
    
}
